package distrcomp;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import javax.rmi.ssl.SslRMIClientSocketFactory;

/*
 * Client class
 */

public class Client {
	public static void main(String[] args) {
		try {
			//Construct some data
			Address src = new Address("Middelheimlaan", "1", "2020", "Antwerpen", "Belgie");
			Address dest = new Address("Burg. Van Hovelaan", "52", "9170", "De Klinge", "Belgie");
			ShippingType type = ShippingType.GROUND;
			Product prod = new Product("iPhone 4S", 649, "EUR", false, 58, 9, 115, 140);
			
			//Get the shipment object from the server
			Registry registry = LocateRegistry.getRegistry(args[0], 1099, new SslRMIClientSocketFactory());
			ShippingQuoter s = (ShippingQuoter) registry.lookup("ShippingService");
			Shipment shipment = s.ship(prod, src, dest, type);
			
			//Request state updates
			ClientTrackerImpl tracker = new ClientTrackerImpl(shipment);
			s.startTracker(tracker, shipment.getID(), null);

			//Test state updates
			while(tracker.getState() != ShippingState.ARRIVED) {
				try {
					Thread.sleep(10000);
				} catch(InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println(tracker.getState());
			}
			
			//Print output
			shipment.print();
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (NotBoundException e) {
			e.printStackTrace();
		}
	}
}
